package org.zhaozhen.org.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zhaozhen.basic.service.impl.BaseServiceImpl;
import org.zhaozhen.basic.util.MD5Utils;
import org.zhaozhen.basic.util.PageList;
import org.zhaozhen.basic.util.StrUtils;
import org.zhaozhen.org.domain.Employee;
import org.zhaozhen.org.domain.Employee;
import org.zhaozhen.org.mapper.EmployeeMapper;
import org.zhaozhen.org.query.EmployeeQuery;
import org.zhaozhen.org.service.IEmployeeService;
import org.zhaozhen.user.domain.Logininfo;
import org.zhaozhen.user.mapper.LogininfoMapper;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    //需要级联处理loginInfo信息
    @Override
    public void add(Employee employee) {
        //设置employee的必要参数  盐值和密码
        initEmployee(employee);
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);//返回自增Id
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    private void initEmployee(Employee employee) {
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        //通过盐值加密密码
        String md5Password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Password);
    }

}
